home *** CD-ROM | disk | FTP | other *** search
/ Game Programming in C++ - Start to Finish / GameProgrammingS.iso / developer_install / CEGUISDK-0.4.1-VC6-Native.exe / {app} / include / renderers / OpenGLGUIRenderer / openglrenderer.h next >
Encoding:
C/C++ Source or Header  |  2005-11-24  |  11.4 KB  |  396 lines

  1. /************************************************************************
  2.     filename:    openglrenderer.h
  3.     created:    9/4/2004
  4.     author:        Mark Strom
  5.                 mwstrom@gmail.com
  6.  
  7.     purpose:    Interface to Renderer implemented via Opengl
  8. *************************************************************************/
  9. /*************************************************************************
  10.     Crazy Eddie's GUI System (http://www.cegui.org.uk)
  11.     Copyright (C)2004 - 2005 Paul D Turner (paul@cegui.org.uk)
  12.  
  13.     This library is free software; you can redistribute it and/or
  14.     modify it under the terms of the GNU Lesser General Public
  15.     License as published by the Free Software Foundation; either
  16.     version 2.1 of the License, or (at your option) any later version.
  17.  
  18.     This library is distributed in the hope that it will be useful,
  19.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  21.     Lesser General Public License for more details.
  22.  
  23.     You should have received a copy of the GNU Lesser General Public
  24.     License along with this library; if not, write to the Free Software
  25.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  26. *************************************************************************/
  27. #ifndef _openglrenderer_h_
  28. #define _openglrenderer_h_
  29.  
  30. #include "CEGUIBase.h"
  31.  
  32. #if defined( __WIN32__ ) || defined( _WIN32 )
  33. #   ifdef OPENGL_GUIRENDERER_EXPORTS
  34. #       define OPENGL_GUIRENDERER_API __declspec(dllexport)
  35. #   else
  36. #       define OPENGL_GUIRENDERER_API __declspec(dllimport)
  37. #   endif
  38. #else
  39. #   define OPENGL_GUIRENDERER_API
  40. #endif
  41.  
  42.  
  43. #if defined(_WIN32)//  All this taken from glut.h
  44. #  ifndef APIENTRY
  45. #   define GLUT_APIENTRY_DEFINED
  46. #   if (_MSC_VER >= 800) || defined(_STDCALL_SUPPORTED) || defined(__BORLANDC__) || defined(__LCC__)
  47. #    define APIENTRY    __stdcall
  48. #   else
  49. #    define APIENTRY
  50. #   endif
  51. #  endif
  52. /* XXX This is from Win32's <winnt.h> */
  53. #  ifndef CALLBACK
  54. #   if (defined(_M_MRX000) || defined(_M_IX86) || defined(_M_ALPHA) || defined(_M_PPC)) && !defined(MIDL_PASS) || defined(__LCC__)
  55. #    define CALLBACK __stdcall
  56. #   else
  57. #    define CALLBACK
  58. #   endif
  59. #  endif
  60. /* XXX Hack for lcc compiler.  It doesn't support __declspec(dllimport), just __stdcall. */
  61. #  if defined( __LCC__ )
  62. #   undef WINGDIAPI
  63. #   define WINGDIAPI __stdcall
  64. #  else
  65. /* XXX This is from Win32's <wingdi.h> and <winnt.h> */
  66. #   ifndef WINGDIAPI
  67. #    define GLUT_WINGDIAPI_DEFINED
  68. #    define WINGDIAPI __declspec(dllimport)
  69. #   endif
  70. #  endif
  71. /* XXX This is from Win32's <ctype.h> */
  72. #  ifndef _WCHAR_T_DEFINED
  73. typedef unsigned short wchar_t;
  74. #   define _WCHAR_T_DEFINED
  75. #  endif
  76. # endif //win32 end glut.h stuff
  77.  
  78. /* XXX Hack for finding headers in Apple's OpenGL framework. */
  79. #if defined( __APPLE__ )
  80. #include <OpenGL/gl.h>
  81. #include <OpenGL/glu.h>
  82. #else /* __APPLE__ */
  83. #include <GL/gl.h>
  84. #include <GL/glu.h>
  85. #endif /* __APPLE__ */
  86. #include <list>
  87. #include <set>
  88.  
  89. #include "CEGUIRenderer.h"
  90. #include "CEGUITexture.h"
  91.  
  92.  
  93. #if defined(_WIN32)
  94. #  if defined(_DEBUG)
  95. #     pragma comment(lib, "CEGUIBase_d.lib")
  96. #  else
  97. #     pragma comment(lib, "CEGUIBase.lib")
  98. #  endif
  99. #endif
  100.  
  101. #if defined(_MSC_VER)
  102. #    pragma warning(push)
  103. #    pragma warning(disable : 4251)
  104. #endif
  105.  
  106. #define OGLRENDERER_VBUFF_CAPACITY    4096
  107.  
  108.  
  109. // Start of CEGUI namespace section
  110. namespace CEGUI
  111. {
  112. /*************************************************************************
  113.     Forward refs
  114. *************************************************************************/
  115. class OpenGLTexture;
  116.  
  117. /*!
  118. \brief
  119. Renderer class to interface with OpenGL
  120. */
  121. class OPENGL_GUIRENDERER_API OpenGLRenderer : public Renderer
  122. {
  123. public:
  124.     /*!
  125.     \brief
  126.         Constructor for OpenGL Renderer object
  127.  
  128.     \param max_quads
  129.         obsolete.  Set to 0.
  130.     */
  131.     OpenGLRenderer(uint max_quads);
  132.     OpenGLRenderer(uint max_quads,int width, int height);
  133.  
  134.  
  135.     /*!
  136.     \brief
  137.         Destructor for OpenGLRenderer objects
  138.     */
  139.     virtual ~OpenGLRenderer(void);
  140.  
  141.     // add's a quad to the list to be rendered
  142.     virtual    void    addQuad(const Rect& dest_rect, float z, const Texture* tex, const Rect& texture_rect, const ColourRect& colours, QuadSplitMode quad_split_mode);
  143.  
  144.     // perform final rendering for all queued renderable quads.
  145.     virtual    void    doRender(void);
  146.  
  147.     // clear the queue
  148.     virtual    void    clearRenderList(void);
  149.  
  150.  
  151.     /*!
  152.     \brief
  153.         Enable or disable the queuing of quads from this point on.
  154.  
  155.         This only affects queuing.  If queuing is turned off, any calls to addQuad will cause the quad to be rendered directly.  Note that
  156.         disabling queuing will not cause currently queued quads to be rendered, nor is the queue cleared - at any time the queue can still
  157.         be drawn by calling doRender, and the list can be cleared by calling clearRenderList.  Re-enabling the queue causes subsequent quads
  158.         to be added as if queuing had never been disabled.
  159.  
  160.     \param setting
  161.         true to enable queuing, or false to disable queuing (see notes above).
  162.  
  163.     \return
  164.         Nothing
  165.     */
  166.     virtual void    setQueueingEnabled(bool setting)        {d_queueing = setting;}
  167.  
  168.  
  169.     // create an empty texture
  170.     virtual    Texture*    createTexture(void);
  171.  
  172.     // create a texture and load it with the specified file.
  173.     virtual    Texture*    createTexture(const String& filename, const String& resourceGroup);
  174.  
  175.     // create a texture and set it to the specified size
  176.     virtual    Texture*    createTexture(float size);
  177.  
  178.     // destroy the given texture
  179.     virtual    void        destroyTexture(Texture* texture);
  180.  
  181.     // destroy all textures still active
  182.     virtual void        destroyAllTextures(void);
  183.  
  184.     /*!
  185.     \brief
  186.         Return whether queuing is enabled.
  187.  
  188.     \return
  189.         true if queuing is enabled, false if queuing is disabled.
  190.     */
  191.     virtual bool    isQueueingEnabled(void) const    {return d_queueing;}
  192.  
  193.  
  194.     /*!
  195.     \brief
  196.         Return the current width of the display in pixels
  197.  
  198.     \return
  199.         float value equal to the current width of the display in pixels.
  200.     */
  201.     virtual float    getWidth(void) const        {return d_display_area.getWidth();}
  202.  
  203.  
  204.     /*!
  205.     \brief
  206.         Return the current height of the display in pixels
  207.  
  208.     \return
  209.         float value equal to the current height of the display in pixels.
  210.     */
  211.     virtual float    getHeight(void) const        {return d_display_area.getHeight();}
  212.  
  213.  
  214.     /*!
  215.     \brief
  216.         Return the size of the display in pixels
  217.  
  218.     \return
  219.         Size object describing the dimensions of the current display.
  220.     */
  221.     virtual Size    getSize(void) const            {return d_display_area.getSize();}
  222.  
  223.  
  224.     /*!
  225.     \brief
  226.         Return a Rect describing the screen
  227.  
  228.     \return
  229.         A Rect object that describes the screen area.  Typically, the top-left values are always 0, and the size of the area described is
  230.         equal to the screen resolution.
  231.     */
  232.     virtual Rect    getRect(void) const            {return d_display_area;}
  233.  
  234.  
  235.     /*!
  236.     \brief
  237.         Return the maximum texture size available
  238.  
  239.     \return
  240.         Size of the maximum supported texture in pixels (textures are always assumed to be square)
  241.     */
  242.     virtual    uint    getMaxTextureSize(void) const        {return d_maxTextureSize;}
  243.  
  244.  
  245.     /*!
  246.     \brief
  247.         Return the horizontal display resolution dpi
  248.  
  249.     \return
  250.         horizontal resolution of the display in dpi.
  251.     */
  252.     virtual    uint    getHorzScreenDPI(void) const    {return 96;}
  253.  
  254.  
  255.     /*!
  256.     \brief
  257.         Return the vertical display resolution dpi
  258.  
  259.     \return
  260.         vertical resolution of the display in dpi.
  261.     */
  262.     virtual    uint    getVertScreenDPI(void) const    {return 96;}
  263.  
  264.  
  265.     /*!
  266.     \brief
  267.         Set the size of the display in pixels.
  268.  
  269.         If your viewport size changes, you can call this function with the new size
  270.         in pixels to update the rendering area.
  271.  
  272.     \note
  273.         This method will cause the EventDisplaySizeChanged event to fire if the
  274.         display size has changed.
  275.  
  276.     \param sz
  277.         Size object describing the size of the display.
  278.  
  279.     \return
  280.         Nothing.
  281.     */
  282.     void    setDisplaySize(const Size& sz);
  283.  
  284.  
  285.     /*!
  286.     \brief
  287.         Grabs all the loaded textures from Texture RAM and stores them in a local data buffer.
  288.         This function invalidates all textures, and restoreTextures must be called before any
  289.         CEGUI rendering is done for predictable results
  290.     */
  291.     void grabTextures(void);
  292.  
  293.  
  294.     /*!
  295.     \brief
  296.         Restores all the loaded textures from the local data buffers previously created by 'grabTextures'
  297.     */
  298.     void restoreTextures(void);
  299.  
  300.  
  301. private:
  302.     /************************************************************************
  303.         Implementation Constants
  304.     ************************************************************************/
  305.     static const int            VERTEX_PER_QUAD;                            //!< number of vertices per quad
  306.     static const int            VERTEX_PER_TRIANGLE;                        //!< number of vertices for a triangle
  307.     static const int            VERTEXBUFFER_CAPACITY;                        //!< capacity of the allocated vertex buffer
  308.     
  309.     /*************************************************************************
  310.         Implementation Structs & classes
  311.     *************************************************************************/
  312.     struct MyQuad
  313.     {
  314.         float tex[2];
  315.         long color;
  316.         float vertex[3];
  317.     };
  318.  
  319.     /*!
  320.     \brief
  321.         structure holding details about a quad to be drawn
  322.     */
  323.     struct QuadInfo
  324.     {
  325.         GLuint        texid;  
  326.         Rect        position;
  327.         float        z;
  328.         Rect        texPosition;
  329.         long        topLeftCol;
  330.         long        topRightCol;
  331.         long        bottomLeftCol;
  332.         long        bottomRightCol;
  333.  
  334.         QuadSplitMode   splitMode;
  335.  
  336.         bool operator<(const QuadInfo& other) const
  337.         {
  338.             // this is intentionally reversed.
  339.             return z > other.z;
  340.         }
  341.  
  342.     };
  343.  
  344.  
  345.     /*************************************************************************
  346.         Implementation Methods
  347.     *************************************************************************/
  348.     // setup states etc
  349.     void    initPerFrameStates(void);
  350.  
  351.     // restore states
  352.     void  exitPerFrameStates(void);
  353.  
  354.     // render whatever is in the vertex buffer
  355.     void    renderVBuffer(void);
  356.  
  357.     // sort quads list according to texture
  358.     void    sortQuads(void);
  359.  
  360.     // render a quad directly to the display
  361.     void    renderQuadDirect(const Rect& dest_rect, float z, const Texture* tex, const Rect& texture_rect, const ColourRect& colours, QuadSplitMode quad_split_mode);
  362.  
  363.     // convert colour value to whatever the OpenGL system is expecting.
  364.     long    colourToOGL(const colour& col) const;
  365.  
  366.     // set the module ID string
  367.     void setModuleIdentifierString();
  368.  
  369.  
  370.     /*************************************************************************
  371.         Implementation Data
  372.     *************************************************************************/
  373.     typedef std::multiset<QuadInfo>        QuadList;
  374.     QuadList d_quadlist;
  375.  
  376.     Rect        d_display_area;
  377.  
  378.     MyQuad        myBuff[OGLRENDERER_VBUFF_CAPACITY];
  379.  
  380.     bool        d_queueing;            //!< setting for queuing control.
  381.     uint        d_currTexture;        //!< Currently bound texture.
  382.     int            d_bufferPos;        //!< index into buffer where next vertex should be put.
  383.     bool        d_sorted;            //!< true when data in quad list is sorted.
  384.  
  385.     std::list<OpenGLTexture*>    d_texturelist;        //!< List used to track textures.
  386.     GLint       d_maxTextureSize;        //!< Holds maximum supported texture size (in pixels).
  387. };
  388.  
  389. } // End of  CEGUI namespace section
  390.  
  391. #if defined(_MSC_VER)
  392. #    pragma warning(pop)
  393. #endif
  394.  
  395. #endif    // end of guard _openglrenderer_h_
  396.